package tech.xs.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import tech.xs.pojo.ClaimVoucher;
import tech.xs.pojo.Employee;
import tech.xs.pojo.Leave;
import tech.xs.service.EmployeeService;
import tech.xs.service.LeaveService;
import tech.xs.util.PaginationSupport;

public class LeaveAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private LeaveService leaveService;
	@Autowired
    private EmployeeService employeeService;
	private int pageNo = 1;
    private int pageSize = 5;
    private Date start;
    public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public Date getStart() {
		return start;
	}
	public void setStart(Date start) {
		this.start = start;
	}
	public Date getStop() {
		return stop;
	}
	public void setStop(Date stop) {
		this.stop = stop;
	}
	public Leave getLeave() {
		return leave;
	}
	public void setLeave(Leave leave) {
		this.leave = leave;
	}
	public Map<String, String> getTypeMap() {
		return typeMap;
	}
	public void setTypeMap(Map<String, String> typeMap) {
		this.typeMap = typeMap;
	}
	public PaginationSupport<ClaimVoucher> getPageSupport() {
		return pageSupport;
	}
	public void setPageSupport(PaginationSupport<ClaimVoucher> pageSupport) {
		this.pageSupport = pageSupport;
	}
	private Date stop;
    private Leave leave;
    private Map<String,String> typeMap = new HashMap<>();
    PaginationSupport<ClaimVoucher> pageSupport = new PaginationSupport<>();
    /**
     * 查看显示
     * @return
     */
    public String list(){
    	Map<String, Object> session = ActionContext.getContext().getSession();
    	Employee employee = (Employee) session.get("employee");
    	PaginationSupport<Leave> pages = new PaginationSupport<>();
    	int count = leaveService.count(employee,start,stop);
    	pages.setTotalCount(count);
    	pages.setCurrPageNo(pageNo);
    	pages.setPageSize(pageSize);  	
    	List<Leave> list = leaveService.list(employee,start,stop, (pages.getCurrPageNo() - 1) * pageSize, pages.getPageSize());
    	pages.setItems(list);
    	session.put("pageSupport", pages);
    	return SUCCESS;
    }
    /**
     * 详情
     * @return
     */
    public String view() {
    	leave = leaveService.getLeave(leave);
    	return SUCCESS;
    }
    
    
    /**
     * 进入添加页面
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public String toadd() {  	
         Map session = ActionContext.getContext().getSession();
         Employee employee = (Employee) session.get("employee");
         employee = employeeService.findEmployee(employee);
         session.put("manager",employee);
    	
        List<String> statusMap1 = leaveService.findLvType();
         typeMap = new HashMap<>();
         for (int i = 0; i < statusMap1.size(); i++) {
        	 typeMap.put(statusMap1.get(i), statusMap1.get(i));
         }
    	return SUCCESS;
    }
   
    /**
     * 添加
     * @return
     */
    public String add() {
    	leave.setCreateTime(new Date());
        leave.setStatus("待审批");
        System.err.println(leave);
        leaveService.save(leave);
        return SUCCESS;
    }

}
